using UnityEngine;

public class CameraTouchOrbit : MonoBehaviour
{
	public static CameraTouchOrbit instance;

	public float xMinLimit;

	public float xMaxLimit;

	private float x;

	private float y;

	private float ratio = 0.5f;

	private Vector2 touchDeltaPosition;

	public bool istrue;

	public bool isfirst;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
	}

	private void Start()
	{
		reset();
	}

	private void Update()
	{
		if (isfirst)
		{
			reset();
			istrue = true;
			isfirst = false;
		}
	}

	private void LateUpdate()
	{
		if (istrue && UnityEngine.Input.touchCount == 1 && UnityEngine.Input.GetTouch(0).phase == TouchPhase.Moved)
		{
			touchDeltaPosition = UnityEngine.Input.GetTouch(0).deltaPosition;
			x += touchDeltaPosition.x * ratio;
			y -= touchDeltaPosition.y * ratio;
			x = ClampAngle(x, xMinLimit, xMaxLimit);
			Quaternion to = Quaternion.Euler(0f, 0f - x, 0f);
			base.transform.rotation = Quaternion.Lerp(base.transform.rotation, to, Time.deltaTime * 3f);
		}
	}

	private static float ClampAngle(float angle, float min, float max)
	{
		if (angle < -360f)
		{
			angle += 360f;
		}
		if (angle > 360f)
		{
			angle -= 360f;
		}
		return Mathf.Clamp(angle, min, max);
	}

	public void reset()
	{
		base.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
		Vector3 eulerAngles = base.transform.eulerAngles;
		x = eulerAngles.y;
		Vector3 eulerAngles2 = base.transform.eulerAngles;
		y = eulerAngles2.x;
	}
}
